-- 时装相关

local GameDB = import(".GameDB")

-- fashion
function GameDB:getClothesConfig(clothesId)
	local clothesConfig = self:getCachedValue("fashion",clothesId)
	return clothesConfig
end

-- 返回整张fashion表
function GameDB:queryClothesConfig()
	--local sql = string.format("SELECT * FROM fashion")
	local clothesConf = self:getCachedValues("fashion")
	return clothesConf
end

-- 获取时装外显配置
function GameDB:getFashionConf(id)
	return self:getClothesResConfig(id) or self:getEquipResConf(id)
end

-- fashion_res
function GameDB:getClothesResConfig(clothesId)
	local clothesResConfig = self:getCachedValue("fashion_res",clothesId)
	return clothesResConfig
end

-- fashion_subkey
function GameDB:getClothesSubConfig( clothesSubId )
	local clothesSubConfig = self:getCachedValue("fashion_subkey",clothesSubId)
	return clothesSubConfig
end

-- fashion_subkey
-- 通过ID找location
function GameDB:getClothesSubLocationFromID( clothesSubId )
	local clothesSubConf = self:getClothesSubConfig( clothesSubId )
	local locationTemp = nil
	if clothesSubConf then
		locationTemp = tonumber( clothesSubConf.location )
	end
	return locationTemp
end

-- 竞技场时装
function GameDB:getArenaFashionIds(sex, classId, isMember)
	local conf = self:getCachedValue("vsEquipment", sex * 10 + classId)
	local ret = {}
	if conf then
		if isMember then
			ret = {tonumber(conf.blueHead), tonumber(conf.blueDress)}
		else
			ret = {tonumber(conf.redHead), tonumber(conf.redDress)}
		end
	end
	return ret
end

-- 追加时装id，会过滤掉相同位置的id
function GameDB:attachFashionIds(fashionIds, attachIds)
	local dict = {}
	for i,id in ipairs(fashionIds) do
		local conf = self:getEquipConf(id)
		conf = conf or self:getClothesConfig(id)
		if conf then
			dict[conf.location] = id
		end
	end

	for i,id in ipairs(attachIds) do
		local conf = self:getEquipConf(id)
		conf = conf or self:getClothesConfig(id)
		if conf then
			dict[conf.location] = id
		end
	end

	local ret = {}
	for k,id in pairs(dict) do
		ret[#ret + 1] = id
	end

	return ret
end

local function checkFashionConfSamePos(confA, confB)
    if confA.toy == "1" and confB.toy == "1" then
    	-- 玩具只在一个位置上，都是玩具，位置肯定相同
    	return true
    else
	    local partA = confA.part1
	    local partB = confB.part1
	    if partA ~= partB then
	        -- 部分外显包含额外头部
	        if (confA.head and partB == "head") or (confB.head and partA == "head") then
	            return true
	        else
	            return false
	        end
	    else
	        return true
	    end
	end
end

-- 追加预览时装id，会替换掉相同部位的id
function GameDB:attachPreviewFashionId(fashionIds, attachId)
	local attachConf = self:getFashionConf(attachId)
	if not attachConf then return fashionIds end

	local ret = {}
	local conf
	for i,id in ipairs(fashionIds) do
		conf = self:getFashionConf(id)
		if conf and not checkFashionConfSamePos(conf, attachConf) then
			ret[#ret + 1] = id
		end
	end
	ret[#ret + 1] = attachId

	return ret
end

-- 追加竞技场时装，原有时装只需要保留主手和副手武器
function GameDB:attachArenaFashionIds(fashionIds, attachIds)
	local dict = {}
	for i,id in ipairs(fashionIds) do
		local conf = self:getEquipConf(id)
		conf = conf or self:getClothesConfig(id)
		-- 只有主手和副手
		if conf and (conf.location == "1" or conf.location == "2") then
			dict[conf.location] = id
		end
	end

	local ret = table.clone(attachIds)
	for k,v in pairs(dict) do
		ret[#ret + 1] = v
	end
	return ret
end

-- fashion_subkey
-- 通过location找ID
function GameDB:getClothesSubIDFromLocation( clothesLocation )
	local config = self:getCachedValue("fashion_subkey",
		function(v)
			return v.location == tostring(clothesLocation)
		end)
	if config then
		return config.ID
	else
		return nil
	end
end

function GameDB:getClothResourceById(id)
	local conf = self:getClothesConfig(id)
	if not conf then return end

	local type
	if tonumber(conf.unlock) == 1 then
		type = Enums.ItemBagType.equip
	elseif tonumber(conf.unlock) == 2 then
		type = Enums.ItemBagType.item
	end
	id = tonumber(conf.unlockValue)
	local res = GD:getConvertResource(conf.track, type, id)
	return res
end

return GameDB